export function fill_zero(num,n){
  while(num.toString().length<n){
    num="0"+num
  }
  return num.toString();
}
export function price_format(_number,n) {
  if(!isNaN(_number)){
    if(!n){
      return _number;
    }  //比如1.5*0.95的结果是1.4249999999999998 用一次四舍五入不能取到正确值
    let number1= Number((Math.round((Math.round(_number*Math.pow(10,n+5))/Math.pow(10,n+5))*Math.pow(10,n))/Math.pow(10,n)));
    return number1.toFixed(n);
  }else{
    return false;
  }
}

let arr=[]
let index=0
let stop
export function fang_pin(time,fun) {
  arr[index]=new Date().getTime()
  if(arr[index]-arr[index-1]<time){
    clearInterval(stop)
  }
  stop= setTimeout(()=>{
    fun()
  },time+2)
  index++
}
export function stop_slider(e,scrollEle,touchStart) {
  let newTouchY=e.touches[0].pageY
  let newTouchX=e.touches[0].pageX
  let isTouchY=Math.abs(newTouchX-touchStart.X)<Math.abs(newTouchY-touchStart.Y)
      if(scrollEle.scrollTop<1&&isTouchY){
         if(newTouchY>touchStart.Y){
            console.log(1)
          e.preventDefault()
           return false
        }
      }
      if(scrollEle.scrollHeight-scrollEle.clientHeight<=scrollEle.scrollTop&&isTouchY){
        if(newTouchY<touchStart.Y){
          console.log(1)
          e.preventDefault()
          return false
        }
      }

}
export function stop_sliderX(e,scrollEle,touchStart) {
  let newTouchX=e.touches[0].pageX
  let newTouchY=e.touches[0].pageY
  let isTouchX=Math.abs(newTouchX-touchStart.X)>Math.abs(newTouchY-touchStart.Y)

      if(scrollEle.scrollLeft<1&&isTouchX){
         if(newTouchX>touchStart.X){
            console.log(1)
          e.preventDefault()
           return false
        }
      }
      if(scrollEle.scrollWidth-scrollEle.clientWidth<=scrollEle.scrollLeft&&isTouchX){
        if(newTouchX<touchStart.X){
          console.log(1)
          e.preventDefault()
          return false
        }
      }

}

/**
 * 将url中问号后的参数转换为对象
 * @param   {String} url    页面完整url
 * @return  {Object} 参数对象
 * */
export function getUrlParamsObject(url) {
  if (url.indexOf('?') === -1) {
    return undefined
  }
  let paraString = url.substring(url.indexOf('?') + 1, url.length)
  let paramsList = paraString.split('&')
  let paraObj = {}
  for (let i = 0; i < paramsList.length; i++) {
    let paramsSubStr = paramsList[i]
    let key = paramsSubStr.substring(0, paramsSubStr.indexOf('='))
    let value = paramsSubStr.substring(paramsSubStr.indexOf('=') + 1, paramsSubStr.length)
    if (value.indexOf('#') > -1) {
      value = value.split('#')[0]
    }
    paraObj[key] = value
  }
  return paraObj
}

/**
 * 判断是否时微信公众号访问
 * @return  {Boolean} true:是微信公众号访问
 * */
export function isWXGZH() {
  let ua = window.navigator.userAgent.toLowerCase()
  return (ua.search(/MicroMessenger/i) !== -1)
}

/**
 * 判断是否时微信公众号访问
 * @param   {Object}    file     图片文件
 * @param   {Number}    quality  图片压缩品质0～1，越小图片压缩的程度越大，越模糊
 * @param   {Function}  callback 回调函数
 * */
export function compressImgFile(file, quality, callback) {
  if (!window.FileReader || !window.Blob) {
    return errorHandler('您的浏览器不支持图片压缩')()
  }

  let reader = new FileReader()
  let mimeType = (file.type || 'image/jpeg') || (file.type || 'image/png') || (file.type || 'image/gif')

  reader.onload = createImage
  reader.onerror = errorHandler('图片读取失败！')
  reader.readAsDataURL(file)

  function createImage() {
    let dataURL = this.result
    let image = new Image()
    image.onload = compressImage
    image.onerror = errorHandler('图片加载失败')
    image.src = dataURL
  }

  function compressImage() {
    let canvas = document.createElement('canvas')
    let ctx, dataURI, result

    canvas.width = this.naturalWidth
    canvas.height = this.naturalHeight
    ctx = canvas.getContext('2d')
    ctx.drawImage(this, 0, 0)
    dataURI = canvas.toDataURL(mimeType, quality)
    result = dataURIToBlob(dataURI)

    callback(null, result)
  }

  function dataURIToBlob(dataURI) {
    let type = dataURI.match(/data:([^;]+)/)[1]
    let base64 = dataURI.replace(/^[^,]+,/, '')
    let byteString = atob(base64)

    let ia = new Uint8Array(byteString.length)
    for (let i = 0; i < byteString.length; i++) {
      ia[i] = byteString.charCodeAt(i)
    }

    return new Blob([ia], {type: type})
  }

  function errorHandler(message) {
    return function () {
      let error = new Error('Compression Error:', message)
      callback(error, null)
    }
  }
}
